using System;
using System.Windows.Forms;
using RollManager.Library;

namespace RollManager.Forms.MainPanels
{
    partial class AdminActionsPanel
    {
        public void SelectNode(string fullPath)
        {
            if( fullPath != null )
            {
                string[] paths = fullPath.Split( new string[] { mainTreeView.PathSeparator }, StringSplitOptions.None );
                mainTreeView.SelectedNode = findNode( paths, 0, mainTreeView.Nodes );
                mainTreeView.SelectedNode.Expand();
            }
        }

        private void refreshTree()
        {
            string lastSelectedNode = null;
            if( mainTreeView.SelectedNode != null)
            {
                lastSelectedNode = mainTreeView.SelectedNode.FullPath;
            }

            mainTreeView.Nodes.Clear();
            addDevLevels();
            addServers();
            addServerGroups();

            // Select the previously selected node.
            SelectNode( lastSelectedNode );
        }

        private TreeNode findNode( string[] paths, int index, TreeNodeCollection nodes )
        {
            if( index >= paths.Length )
            {
                return null;
            }

            TreeNode[] subNodes = nodes.Find( paths[ index ], false );
            if( subNodes != null && subNodes.Length > 0 )
            {
                TreeNode node = findNode( paths, index + 1, subNodes[0].Nodes );
                if( node == null )
                {
                    return subNodes[ 0 ];
                }
                return node;
            }
            return null;
        }

        private void addServers()
        {
            // create root node
            TreeNode rootNode = createTreeNode( "Servers" );
            mainTreeView.Nodes.Add( rootNode );
        }

        private static TreeNode createTreeNode( string name )
        {
            TreeNode node = new TreeNode( name );
            node.Name = name;
            return node;
        }

        private void addServerGroups()
        {
            // create root node
            TreeNode rootNode = createTreeNode( "Server Groups" );
            mainTreeView.Nodes.Add( rootNode );

            foreach( ServerGroup group in Database.GetServerGroups() )
            {
                TreeNode node = createTreeNode( group.Name );
                node.Tag = group;
                rootNode.Nodes.Add( node );
            }
        }

        private void addDevLevels()
        {
            // create root node
            TreeNode rootNode = createTreeNode( "Dev Levels" );
            mainTreeView.Nodes.Add( rootNode );

            foreach( DevLevel devLevel in Database.GetDevLevels() )
            {
                TreeNode node = createTreeNode( devLevel.Name );
                node.Tag = devLevel;

                addActionNodes( node.Nodes, devLevel );

                rootNode.Nodes.Add( node );
            }
        }

        private static void addActionNodes( TreeNodeCollection nodes, DevLevel devLevel )
        {
            foreach( RollManager.Library.Action action in Database.GetActions( devLevel.Id ) )
            {
                TreeNode node = createTreeNode( action.Name );
                node.Tag = action;

                addSubActionNodes( node.Nodes, action );

                nodes.Add( node );
            }
        }

        private static void addSubActionNodes( TreeNodeCollection nodes, RollManager.Library.Action action )
        {
            foreach( SubAction subAction in Database.GetSubActions( action.Id ) )
            {
                TreeNode node = createTreeNode( subAction.Name );
                node.Tag = subAction;
                nodes.Add( node );
            }
        }

        public void AddNode( string name, object tag )
        {
            TreeNode node = createTreeNode( name );
            node.Tag = tag;
            mainTreeView.SelectedNode.Nodes.Add( node );
        }

        private void deleteCurrentNode()
        {
            TreeNode node = mainTreeView.SelectedNode;
            mainTreeView.SelectedNode = node.Parent;
            node.Remove();

        }
    }
}